<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:gml="http://www.opengis.net/gml" 
	xmlns:ogr="http://ogr.maptools.org/" 
	xmlns:ramon_geo="http://ec.europa.eu/eurostat/ramon/ontologies/geographic.rdf#" 
	xmlns:ramon="http://rdfdata.eionet.europa.eu/ramon/ontology/"
	xmlns:fao="http://aims.fao.org/aos/geopolitical.owl#"
	xmlns:owl="http://www.w3.org/2002/07/owl#"
	xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
	xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
	
<xsl:output method="text" indent="yes" />
<xsl:template match="/ogr:FeatureCollection">

	<xsl:variable name="dbpedia_dump" select="document('../sources/dbpedia/nuts_dbpedia_settlements_over_10000.xml')"/>
	<xsl:variable name="nuts_dump" select="document('../sources/ramon/nuts2008.rdf')"/>
	<xsl:variable name="eionet_dump" select="document('../sources/eionet/eionet.rdf')"/>	
	<xsl:variable name="fao_dump" select="document('../sources/fao/geopolitical.owl')"/>	
	<xsl:variable name="ukos_mappings" select="document('../sources/ukos/ukos_nuts_seeAlso.rdf')"/>		
	<xsl:variable name="geoes_mappings" select="document('../sources/geoes/geoes_nuts_seeAlso.rdf')"/>	
	<xsl:variable name="touches_relations" select="document('../sources/touches.xml')"/>	

	<xsl:for-each select='gml:featureMember/ogr:NUTS_RG_60M_2006' >

		<xsl:variable name="nuts_id" select="ogr:NUTS_ID"/>
		<xsl:variable name="nuts_uri" select="concat('http://nuts.geovocab.org/id/',$nuts_id)"/>
		<xsl:variable name="eionet_uri" select="concat('http://rdfdata.eionet.europa.eu/eea/countries/',$nuts_id)"/>
	
		<xsl:result-document href="{concat('id/',ogr:NUTS_ID,'.ttl')}">
@prefix rdf: &lt;http://www.w3.org/1999/02/22-rdf-syntax-ns#&gt; .
@prefix rdfs: &lt;http://www.w3.org/2000/01/rdf-schema#&gt; .
@prefix nuts: &lt;http://nuts.geovocab.org/id/&gt; .
@prefix ramon: &lt;http://rdfdata.eionet.europa.eu/ramon/ontology/&gt; .
@prefix ngeo: &lt;http://geovocab.org/geometry#&gt; .
@prefix spatial: &lt;http://geovocab.org/spatial#&gt; .
@prefix owl: &lt;http://www.w3.org/2002/07/owl#&gt; .

			<xsl:apply-templates select="ogr:geometryProperty">
				<xsl:with-param name="dbpedia_nodes" select="$dbpedia_dump/table/row[column[position()=1]=$nuts_uri]" />
				<xsl:with-param name="nuts_node" select="$nuts_dump/rdf:RDF/ramon_geo:NUTSRegion[@rdf:about=$nuts_id]" />
				<xsl:with-param name="eionet_node" select="$eionet_dump/rdf:RDF/rdf:Description[@rdf:about=$eionet_uri]" />
				<xsl:with-param name="fao_node" select="$fao_dump/rdf:RDF/fao:self_governing[fao:codeISO2=$nuts_id]" />
				<xsl:with-param name="ukos_node" select="$ukos_mappings/rdf:RDF/rdf:Description[@rdf:about=$nuts_uri]"/>	
				<xsl:with-param name="geoes_node" select="$geoes_mappings/rdf:RDF/rdf:Description[@rdf:about=$nuts_uri]"/>	
				<xsl:with-param name="touches_nodes" select="$touches_relations/table/row[column[position()=1]=$nuts_uri]" />
				<xsl:with-param name="nuts_id" select="$nuts_id" />
			</xsl:apply-templates>
		</xsl:result-document>

	</xsl:for-each>

</xsl:template>

<xsl:template name="geometry" match="ogr:geometryProperty">

	<xsl:param name="dbpedia_nodes" />
	<xsl:param name="nuts_node" />
	<xsl:param name="eionet_node" />
	<xsl:param name="fao_node" />
	<xsl:param name="ukos_node" />
	<xsl:param name="geoes_node" />
	<xsl:param name="touches_nodes" />
	<xsl:param name="nuts_id" />

	<xsl:variable name="geometry_id" select="concat('nuts:',$nuts_id,'_geometry')"/>
	
nuts:<xsl:value-of select="$nuts_id" /> rdf:type ramon:NUTSRegion, spatial:Feature .
nuts:<xsl:value-of select="$nuts_id" /> rdfs:label "<xsl:value-of select="$nuts_node/rdfs:label" />" .
nuts:<xsl:value-of select="$nuts_id" /> ramon:name "<xsl:value-of select="$nuts_node/ramon_geo:name" />" .
nuts:<xsl:value-of select="$nuts_id" /> ramon:level "<xsl:value-of select="$nuts_node/ramon_geo:level" />"^^&lt;http://www.w3.org/2001/XMLSchema#integer&gt; .
nuts:<xsl:value-of select="$nuts_id" /> ramon:code "<xsl:value-of select="$nuts_node/ramon_geo:code" />" .
nuts:<xsl:value-of select="$nuts_id" /> ngeo:geometry <xsl:value-of select="$geometry_id" /> .
<xsl:if test="$nuts_node/ramon_geo:hasParentRegion">
nuts:<xsl:value-of select="$nuts_id" /> spatial:PP nuts:<xsl:value-of select="$nuts_node/ramon_geo:hasParentRegion/@rdf:resource" /> .
</xsl:if>
<xsl:if test="$fao_node">
<xsl:for-each select='$fao_node/fao:hasBorderWith' >
<xsl:variable name="ec_node" select="substring-after(./@rdf:resource, '#')"/>
<xsl:variable name="codeISO2" select="../../fao:self_governing[@rdf:ID=$ec_node]/fao:codeISO2"/>
<xsl:if test="$nuts_node/../ramon_geo:NUTSRegion[@rdf:about=$codeISO2]">
nuts:<xsl:value-of select="$nuts_id" /> spatial:EC nuts:<xsl:value-of select="$codeISO2" /> .
</xsl:if>
</xsl:for-each>
</xsl:if>
<xsl:if test="$dbpedia_nodes">
<xsl:for-each select='$dbpedia_nodes' >
nuts:<xsl:value-of select="$nuts_id" /> spatial:PPi &lt;<xsl:value-of select="column[position()=last()]"/>&gt; .
</xsl:for-each>
</xsl:if>
<xsl:if test="$touches_nodes">
<xsl:for-each select='$touches_nodes' >
nuts:<xsl:value-of select="$nuts_id" /> spatial:EC &lt;<xsl:value-of select="column[position()=last()]"/>&gt; .
</xsl:for-each>
</xsl:if>
nuts:<xsl:value-of select="$nuts_id" /> owl:sameAs &lt;<xsl:value-of select="concat('http://rdfdata.eionet.europa.eu/ramon/nuts2008/',$nuts_id)" />&gt; .
nuts:<xsl:value-of select="$nuts_id" /> owl:sameAs &lt;<xsl:value-of select="concat('http://ec.europa.eu/eurostat/ramon/rdfdata/nuts2008/',$nuts_id)" />&gt; .
nuts:<xsl:value-of select="$nuts_id" /> owl:sameAs &lt;<xsl:value-of select="concat('http://estatwrap.ontologycentral.com/dic/geo#',$nuts_id)" />&gt; .
nuts:<xsl:value-of select="$nuts_id" /> owl:sameAs &lt;<xsl:value-of select="concat('http://nuts.psi.enakting.org/id/',$nuts_id)" />&gt; .
<xsl:if test="$eionet_node">
<xsl:for-each select='$eionet_node/owl:sameAs' >
nuts:<xsl:value-of select="$nuts_id" /> owl:sameAs &lt;<xsl:value-of select="./@rdf:resource" />&gt; .
</xsl:for-each>
</xsl:if>
<xsl:if test="$ukos_node">
<xsl:for-each select='$ukos_node/rdfs:seeAlso' >
nuts:<xsl:value-of select="$nuts_id" /> rdfs:seeAlso &lt;<xsl:value-of select="./@rdf:resource" />&gt; .
</xsl:for-each>
</xsl:if>
<xsl:if test="$geoes_node">
<xsl:for-each select='$geoes_node/rdfs:seeAlso' >
nuts:<xsl:value-of select="$nuts_id" /> rdfs:seeAlso &lt;<xsl:value-of select="./@rdf:resource" />&gt; .
</xsl:for-each>
</xsl:if>



	
</xsl:template>

</xsl:stylesheet>